{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "stock_basic 表中的数据:\n",
      "        ts_code  symbol     name  area industry cnspell market list_date  \\\n",
      "0     000001.SZ  000001     平安银行    深圳       银行    payh     主板  19910403   \n",
      "1     000002.SZ  000002      万科A    深圳     全国地产     wka     主板  19910129   \n",
      "2     000004.SZ  000004     国华网安    深圳     软件服务    ghwa     主板  19910114   \n",
      "3     000006.SZ  000006     深振业A    深圳     区域地产    szya     主板  19920427   \n",
      "4     000007.SZ  000007      全新好    深圳     其他商业     qxh     主板  19920413   \n",
      "...         ...     ...      ...   ...      ...     ...    ...       ...   \n",
      "5390  920111.BJ  920111     聚星科技  None     None    jxkj    北交所  20241111   \n",
      "5391  920116.BJ  920116     星图测控  None     None    xtck    北交所  20250102   \n",
      "5392  920118.BJ  920118     太湖远大  None     None    thyd    北交所  20240822   \n",
      "5393  920128.BJ  920128     胜业电气  None     None    sydq    北交所  20241129   \n",
      "5394  689009.SH  689009  九号公司-WD    北京      摩托车    jhgs    科创板  20201029   \n",
      "\n",
      "                act_name act_ent_type  \n",
      "0                 无实际控制人            无  \n",
      "1     深圳市人民政府国有资产监督管理委员会         地方国企  \n",
      "2                    李映彤         民营企业  \n",
      "3     深圳市人民政府国有资产监督管理委员会         地方国企  \n",
      "4                    王玩虹         民营企业  \n",
      "...                  ...          ...  \n",
      "5390                None         None  \n",
      "5391                None         None  \n",
      "5392                None         None  \n",
      "5393                None         None  \n",
      "5394                None         None  \n",
      "\n",
      "[5395 rows x 10 columns]\n",
      "000009.sz 的每日价格数据:\n",
      "         ts_code trade_date  open  high   low  close  pre_close  change  \\\n",
      "0      000009.SZ   20250124  8.54  8.63  8.51   8.54       8.53    0.01   \n",
      "1      000009.SZ   20250123  8.65  8.77  8.52   8.53       8.56   -0.03   \n",
      "2      000009.SZ   20250122  8.64  8.64  8.46   8.56       8.64   -0.08   \n",
      "3      000009.SZ   20250121  8.80  8.83  8.60   8.64       8.72   -0.08   \n",
      "4      000009.SZ   20250120  8.71  8.85  8.67   8.72       8.66    0.06   \n",
      "...          ...        ...   ...   ...   ...    ...        ...     ...   \n",
      "17995  000009.SZ   19990623  4.10  4.25  4.10   4.17       4.10    0.07   \n",
      "17996  000009.SZ   19990622  4.02  4.10  3.95   4.10       4.00    0.10   \n",
      "17997  000009.SZ   19990621  3.90  4.03  3.83   4.00       3.90    0.10   \n",
      "17998  000009.SZ   19990618  3.96  3.97  3.79   3.90       3.99   -0.09   \n",
      "17999  000009.SZ   19990617  4.12  4.15  3.92   3.99       4.12   -0.13   \n",
      "\n",
      "       pct_chg        vol       amount  \n",
      "0       0.1172  118421.43  101515.6260  \n",
      "1      -0.3505  129813.41  112114.1810  \n",
      "2      -0.9259  103666.08   88498.8640  \n",
      "3      -0.9174   85851.74   74322.3370  \n",
      "4       0.6928  107580.98   93976.1900  \n",
      "...        ...        ...          ...  \n",
      "17995   1.7100  142631.00   59295.0560  \n",
      "17996   2.5000  114696.00   46251.6705  \n",
      "17997   2.5600  120029.00   47449.0672  \n",
      "17998  -2.2600  111478.00   42878.8067  \n",
      "17999  -3.1600  124205.00   49311.2962  \n",
      "\n",
      "[18000 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "# 创建数据库引擎\n",
    "engine = create_engine('mysql+pymysql://root:shx20050507@localhost/stock_basic')\n",
    "\n",
    "# 查询 stock_basic 表\n",
    "sql = \"SELECT * FROM stock_basic\"\n",
    "df_stock_basic = pd.read_sql(sql, engine)\n",
    "print(\"stock_basic 表中的数据:\")\n",
    "print(df_stock_basic)\n",
    "\n",
    "# 查询特定股票的每日价格数据\n",
    "sql = \"SELECT * FROM `000009.sz`\"\n",
    "df_daily_prices = pd.read_sql(sql, engine)\n",
    "print(\"000009.sz 的每日价格数据:\")\n",
    "print(df_daily_prices)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
